﻿using System.Collections.Generic;
using System.Linq;
using DomainModel;
using ForumEngine.Models;
using ObjectModel;

namespace ViewServices
{
    class PathViewService : IPathViewService
    {
        private readonly IConfiguration _configuration;

        public PathViewService(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        public PathModel GetPathRoot()
        {
            return new PathModel { Name = _configuration.ForumName, Url = MvcHelper.UrlHelper.Content("~") };
        }

        public PathModel GetPathSections()
        {
            return new PathModel { Name = "Sections" };
        }

        public TopTabModel GetTopTabs()
        {
            var model = new TopTabModel
            {
                Applications = new List<TopTabApplicationModel>(),
                MenuItems = new List<TopTabMenuItemModel>()
            };

            using (var domainModel = new DataAccess.DomainModel())
            {
                model.CurrentApplicationName = (string)MvcHelper.RequestContext.RouteData.Values["application"];

                Application currentApplication = null;

                foreach (var application in domainModel.Applications.Where(a => a.Enabled && a.Visible))
                {
                    if (application.Name == model.CurrentApplicationName)
                    {
                        currentApplication = application;
                    }
                    model.Applications.Add(new TopTabApplicationModel { Id = application.Id, Name = application.Name, Url = MvcHelper.UrlHelper.RouteUrl(new { application = application.Name }) });
                }

                if (currentApplication != null)
                {
                    foreach (var menuItem in currentApplication.MenuItems)
                    {
                        model.MenuItems.Add(new TopTabMenuItemModel { Id = menuItem.Id, Name = menuItem.Name, Url = MvcHelper.UrlHelper.RouteUrl(new { application = currentApplication.Name, menu = menuItem.Name }) });
                    }
                }
            }

            return model;
        }

        private PathModel GetPathSection(Section section, bool isCurrent)
        {
            return new PathModel
            {
                Name = section.Name,
                Url = isCurrent ? null : MvcHelper.UrlHelper.RouteUrl(new { application = "Forum", action = "Section", id = section.Id })
            };
        }

        private void GetFullPathSection(Section section, List<PathModel> path)
        {
            section = section.Parent;

            if (section == null)
            {
                return;
            }

            path.Insert(0, GetPathSection(section, false));

            GetFullPathSection(section, path);
        }

        public List<PathModel> GetFullPathSection(long sectionId, bool isCurrent)
        {
            Section section;

            using (var domainModel = new DataAccess.DomainModel())
            {
                section = domainModel.Sections.Find(sectionId);
            }

            var ret = new List<PathModel> { GetPathSection(section, isCurrent) };
            GetFullPathSection(section, ret);
            ret.Insert(0, GetPathSections());
            return ret;
        }

        public List<PathModel> GetFullPathThread(long threadId)
        {
            Thread thread;

            using (var domainModel = new DataAccess.DomainModel())
            {
                thread = domainModel.Threads.Find(threadId);
            }

            var ret = new List<PathModel>();
            ret.AddRange(GetFullPathSection(thread.Section.Id, false));
            ret.Add(new PathModel { Name = thread.Name });
            return ret;
        }
    }
}
